import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Canvas;
import java.util.Vector;

public class SplashActionHandler implements Runnable
{
	private SplashIIMIDlet mid;
	private int action = -1;
	private boolean waitfor = false;
	private SplashConnection scn;
	
	public SplashActionHandler(SplashIIMIDlet m)
	{
		this.mid = m;
	}
	
	public void run()
	{
		while(true)
		{
			/* Action handling */
			if(action != -1)
			{
				act(action);
				action = -1;
			}
			
			try
			{
				Thread.sleep(10);
			}
			catch(InterruptedException e) {}
				
		}
	}
	
	public void start()
	{
		new Thread(this).start();
	}
	
	public boolean setAction(int action)
	{
		System.out.println("SetAction " + action);
		/*if(this.action != -1)
			return false;
		*/
		
		this.action = action;
		System.out.println("Nastavljena na" + this.action);
		return true;
	}
	
	private void act(int itemId)
	{
		System.out.println("Action called " + itemId + " on form " + mid.currentForm);
		switch(mid.currentForm)
		{
			/* Main menu subforms */
			case FormConstants.MENU_FORM:
				switch(itemId)
				{
					case 0:
						mid.setScreen(mid.messageForm(), FormConstants.MESSAGE_FORM);
					break;
					case 1:
						mid.setScreen(mid.shutdownForm(), FormConstants.SHUTDOWN);
					break;
					case 2:
						mid.setScreen(mid.executeForm(), FormConstants.EXECUTE);
					break;
					case 3:
						mid.setScreen(new SplashConnection(mid, "zahtevam sliko", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.REQUEST_IMAGE);
					
						scn = (SplashConnection)mid.current;
						scn.addHeader("ssid", mid.ssid);
						scn.addHeader("mid", "1");
						scn.addHeader("cmd", "screen");
						
						/* horizontal view? */
						if(mid.current.getHeight() > mid.current.getWidth())
						{
							scn.addHeader("width", String.valueOf(mid.current.getHeight()));
							scn.addHeader("height", String.valueOf(mid.current.getWidth()));
						}
						else
						{
							scn.addHeader("width", String.valueOf(mid.current.getWidth()));
							scn.addHeader("height", String.valueOf(mid.current.getHeight()));
						}
						
						scn.connect();
					break;
					case 4:
						mid.setScreen(new SplashConnection(mid, "zaustavljam streznik", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.EXIT_SERVER);
					
						scn = (SplashConnection)mid.current;
						scn.addHeader("ssid", mid.ssid);
						scn.addHeader("mid", "1");
						scn.addHeader("cmd", "exit");
						scn.connect();
					break;
					case 5:
						mid.setScreen(new SplashPresentation(mid), FormConstants.PRESENTATION_MENU);
					break;
					case 6:
						mid.setScreen(new SplashConnection(mid, "zahtevam misko", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.REQUEST_MOUSE);
					
						scn = (SplashConnection)mid.current;
						scn.addHeader("ssid", mid.ssid);
						scn.addHeader("mid", "1");
						scn.addHeader("cmd", "mouse");
						scn.connect();
					break;
					case 7:
						mid.setScreen(new SplashConnection(mid, "zahtevam datoteke", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.REQUEST_FILESYSTEM);
					
						scn = (SplashConnection)mid.current;
						scn.addHeader("ssid", mid.ssid);
						scn.addHeader("mid", "1");
						scn.addHeader("cmd", "read");
						scn.addHeader("file", ".");
						scn.connect();
					break;
					case 8:
						mid.notifyDestroyed();
					break;
					case 9:
						mid.setScreen(new About(mid), FormConstants.ABOUT);
					break;
				}
			break;
			/* Main menu - END */
			case FormConstants.FILE_LIST:
				if(itemId == FormConstants.EXIT)
				{
					mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
				}
				else
				{
					String request = ((SplashFileList)mid.current).getRequested();
					System.out.println("requested file: " + mid.urlEncode(request));
					mid.setScreen(new SplashConnection(mid, "zahtevam datoteke",
						"socket://" + mid.ip + ":42390"),
					FormConstants.REQUEST_FILESYSTEM);
				
					scn = (SplashConnection)mid.current;
					scn.addHeader("ssid", mid.ssid);
					scn.addHeader("mid", "1");
					scn.addHeader("cmd", "read");
					scn.addHeader("file", mid.urlEncode(request));
					scn.connect();
				}
			break;
			case FormConstants.REQUEST_FILESYSTEM:
				scn = (SplashConnection)mid.current;
				String tmstr;
				
				if(scn.isDone())
				{
					Vector tempv = scn.getResponseHeaders();
					int status = 0;
					int port = 0;
					for(int i = 0; i < tempv.size(); i += 2)
					{
						/* It is case sensitive */
						tmstr = ((StringBuffer)tempv.elementAt(i)).toString();
						
						if(tmstr.equals("status"))
							status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
						if(tmstr.equals("port"))
							port = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
					}
					
					if(status == 11 && port > 0 && port < 65536)
					{
						/* Load list */
						mid.setScreen(new SplashFileList(mid), FormConstants.FILE_LIST);
						((SplashFileList)mid.current).load(
							"socket://" + mid.ip + ":" + port);
					}
					else
					{
						System.out.println("Status:" + status);
						/* Display error screen */
						mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Prislo je do napake."),
									FormConstants.ALERT);
					}
				}
				else
				{
					/* request timed out */
					mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
									FormConstants.ALERT);
				}
			break;
			case FormConstants.EXIT_SERVER:
				scn = (SplashConnection)mid.current;
				String tempst;
				
				if(scn.isDone())
				{
					Vector tempv = scn.getResponseHeaders();
					int status = 0;
					for(int i = 0; i < tempv.size(); i += 2)
					{
						/* It is case sensitive */
						tempst = ((StringBuffer)tempv.elementAt(i)).toString();
						
						if(tempst.equals("status"))
							status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
					}
					
					if(status == 11)
					{
						mid.setScreen(new SplashAlert(mid, SplashAlert.OK, "Streznik je sedaj zaustavljen"),
									FormConstants.ALERT);
					}
					else
					{
						/* Display error screen */
						mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Prislo je do napake."),
									FormConstants.ALERT);
					}
				}
				else
				{
					/* request timed out */
					mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
									FormConstants.ALERT);
				}
			break;
			case FormConstants.REQUEST_MOUSE:
				scn = (SplashConnection)mid.current;
				String tempstr;
				
				if(scn.isDone())
				{
					Vector tempv = scn.getResponseHeaders();
					int status = 0;
					int port = 0;
					for(int i = 0; i < tempv.size(); i += 2)
					{
						/* It is case sensitive */
						tempstr = ((StringBuffer)tempv.elementAt(i)).toString();
						
						if(tempstr.equals("status"))
							status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
						if(tempstr.equals("port"))
							port = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
					}
					
					if(status == 11 && port > 0 && port < 65536)
					{
						mid.setScreen(new SplashMouse(mid, "socket://" + mid.ip + ":" + port), FormConstants.MOUSE_MENU);
						new Thread(((SplashMouse)mid.current)).start();
					}
					else
					{
						System.out.println("STATUS = " + status + " & PORT = " + port);
						/* Display error screen */
						mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Prislo je do napake."),
									FormConstants.ALERT);
					}
				}
				else
				{
					/* request timed out */
					mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
									FormConstants.ALERT);
				}
			break;
			case FormConstants.MOUSE_MENU:
				mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
			break;
			case FormConstants.ABOUT:
				mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
			break;
			case FormConstants.PRESENTATION_MENU:
				mid.setScreen(new SplashConnection(mid, "posiljam", "socket://" + 
				mid.ip + ":42390"),
				FormConstants.PRESENTATION_PROC);
			
				scn = (SplashConnection)mid.current;
				scn.addHeader("ssid", mid.ssid);
				scn.addHeader("mid", "1");
				scn.addHeader("cmd", "presentation");
				
				switch(itemId)
				{
					case Canvas.KEY_NUM4:
						scn.addHeader("press", "left");
					break;
					case Canvas.KEY_NUM8:
						scn.addHeader("press", "down");
					break;
					case Canvas.KEY_NUM6:
						scn.addHeader("press", "right");
					break;
					case Canvas.KEY_NUM2:
						scn.addHeader("press", "up");
					break;
					case Canvas.KEY_NUM0:
						scn.addHeader("press", "exit");
					break;
					case Canvas.KEY_NUM7:
						scn.addHeader("press", "white");
					break;
					/* Must be nice being' white, I'm just kiddin' black guys :P */
					case Canvas.KEY_NUM9:
						scn.addHeader("press", "black");
					break;
					default:
						mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
					break;
				}
				scn.connect();
			break;
			case FormConstants.PRESENTATION_PROC:
				switch(itemId)
				{
					case 0:
						/* Any key to cancel */
						((SplashConnection)mid.current).cancelConnection();
						mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
					break;
					case FormConstants.TRANSACTION_END:
						scn = (SplashConnection)mid.current;
						String temps;
						
						if(scn.isDone())
						{
							Vector tempv = scn.getResponseHeaders();
							int status = 0;
							for(int i = 0; i < tempv.size(); i += 2)
							{
								/* It is case sensitive */
								temps = ((StringBuffer)tempv.elementAt(i)).toString();
								
								if(temps.equals("status"))
									status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
							}
							
							if(status == 11)
							{
								mid.setScreen(new SplashPresentation(mid), FormConstants.PRESENTATION_MENU);
							}
							else
							{
								/* Display error screen */
								mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Prislo je do napake."),
											FormConstants.ALERT);
							}
						}
						else
						{
							/* request timed out */
							mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
											FormConstants.ALERT);
						}
					break;
				}
			break;
			case FormConstants.REQUEST_IMAGE:
				/* loadimage */
				switch(itemId)
				{
					case 0:
						/* Any key to cancel */
						((SplashConnection)mid.current).cancelConnection();
						mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
					break;
					case FormConstants.TRANSACTION_END:
						scn = (SplashConnection)mid.current;
						String temps;
						
						if(scn.isDone())
						{
							Vector tempv = scn.getResponseHeaders();
							int status = 0;
							int port = 0;
							int size = 0;
							for(int i = 0; i < tempv.size(); i += 2)
							{
								/* It is case sensitive */
								temps = ((StringBuffer)tempv.elementAt(i)).toString();
								
								if(temps.equals("status"))
									status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
								else if(temps.equals("port"))
									port = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
								else if(temps.equals("size"))
									size = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
							}
							
							if(status == 11 && port > 0 && port < 65536)
							{
								/* It is OK */
								/* Load image */
								mid.setScreen(new SplashImage(mid, "nalagam...", "socket://" + mid.ip + ":" + port, size),
															FormConstants.LOAD_IMAGE);
								SplashImage si = (SplashImage)mid.current;
								si.connect();
							}
							else
							{
								/* Display error screen */
								mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Zaganjanje programa ni uspelo."),
											FormConstants.ALERT);
							}
						}
						else
						{
							/* request timed out */
							mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
											FormConstants.ALERT);
						}
					break;
				}
			break;
			case FormConstants.LOAD_IMAGE:
				/* we've seen enough! let's go! */
				mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
			break;
			case FormConstants.EXECUTE:
				if(itemId == FormConstants.OK)
				{
					/* send */
					SplashForm sf = (SplashForm)mid.current;
					mid.setScreen(new SplashConnection(mid, "zaganjam", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.EXECUTE_PROC);
					
					scn = (SplashConnection)mid.current;
					scn.addHeader("ssid", mid.ssid);
					scn.addHeader("mid", "1");
					scn.addHeader("cmd", "execute");
					scn.addHeader("command", ((TextField)sf.get(0)).getString());
					scn.connect();
					
				}
				else
				{
					mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
				}
			break;
			case FormConstants.EXECUTE_PROC:
				switch(itemId)
				{
					case 0:
						/* Any key to cancel */
						System.out.println("Form cancel");
						((SplashConnection)mid.current).cancelConnection();
						mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
					break;
					case FormConstants.TRANSACTION_END:
						scn = (SplashConnection)mid.current;
						String temps;
						
						if(scn.isDone())
						{
							Vector tempv = scn.getResponseHeaders();
							int status = 0;
							for(int i = 0; i < tempv.size(); i += 2)
							{
								/* It is case sensitive */
								temps = ((StringBuffer)tempv.elementAt(i)).toString();
								
								if(temps.equals("status"))
									status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
							}
							
							if(status == 11)
							{
								/* It is OK */
								mid.setScreen(new SplashAlert(mid, SplashAlert.OK, "Program pognan."),
											FormConstants.ALERT);
							}
							else
							{
								/* Display error screen */
								mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Zaganjanje programa ni uspelo."),
											FormConstants.ALERT);
							}
						}
						else
						{
							/* request timed out */
							mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
											FormConstants.ALERT);
						}
					break;
				}
			break;
			case FormConstants.SHUTDOWN:
				if(itemId == FormConstants.OK)
				{
					/* send */
					SplashForm sf = (SplashForm)mid.current;
					mid.setScreen(new SplashConnection(mid, "zaustavljam", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.SHUTDOWN_PROC);
					
					scn = (SplashConnection)mid.current;
					scn.addHeader("ssid", mid.ssid);
					scn.addHeader("mid", "1");
					scn.addHeader("cmd", "shutdown");
					switch(((ChoiceGroup)sf.get(0)).getSelectedIndex())
					{
						case 0:
							scn.addHeader("type", "shutdown");
						break;
						case 1:
							scn.addHeader("type", "reboot");
						break;
						case 2:
							scn.addHeader("type", "cancel");
						break;
					}
					scn.connect();
					
				}
				else
				{
					mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
				}
			break;
			case FormConstants.SHUTDOWN_PROC:
				switch(itemId)
				{
					case 0:
						/* Any key to cancel */
						System.out.println("Form cancel");
						((SplashConnection)mid.current).cancelConnection();
						mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
					break;
					case FormConstants.TRANSACTION_END:
						scn = (SplashConnection)mid.current;
						String temps;
						
						if(scn.isDone())
						{
							Vector tempv = scn.getResponseHeaders();
							int status = 0;
							for(int i = 0; i < tempv.size(); i += 2)
							{
								/* It is case sensitive */
								temps = ((StringBuffer)tempv.elementAt(i)).toString();
								
								if(temps.equals("status"))
									status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
							}
							
							if(status == 11)
							{
								/* It is OK */
								mid.setScreen(new SplashAlert(mid, SplashAlert.OK, "Racunalnik se bo zaustavil."),
											FormConstants.ALERT);
							}
							else
							{
								/* Display error screen */
								mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Zaustavitev ni uspela."),
											FormConstants.ALERT);
							}
						}
						else
						{
							/* request timed out */
							mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
											FormConstants.ALERT);
						}
					break;
				}
			break;
			case FormConstants.MESSAGE_FORM:
				if(itemId == FormConstants.OK)
				{
					/* send */
					SplashForm sf = (SplashForm)mid.current;
					mid.setScreen(new SplashConnection(mid, "posiljam", "socket://" + 
						mid.ip + ":42390"),
						FormConstants.SEND_MESSAGE);
					
					scn = (SplashConnection)mid.current;
					scn.addHeader("ssid", mid.ssid);
					scn.addHeader("mid", "1");
					scn.addHeader("cmd", "message");
					scn.addHeader("msg", mid.urlEncode(((TextField)sf.get(0)).getString()));
					scn.addHeader("from", mid.urlEncode(((TextField)sf.get(1)).getString()));
					switch(((ChoiceGroup)sf.get(2)).getSelectedIndex())
					{
						case 0:
							scn.addHeader("type", "msg");
						break;
						case 1:
							scn.addHeader("type", "war");
						break;
						case 2:
							scn.addHeader("type", "err");
						break;
					}
					scn.connect();
					
				}
				else
				{
					mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
				}
			break;
			case FormConstants.SEND_MESSAGE:
				switch(itemId)
				{
					case 0:
						/* Any key to cancel */
						System.out.println("Form cancel");
						((SplashConnection)mid.current).cancelConnection();
						mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
					break;
					case FormConstants.TRANSACTION_END:
						scn = (SplashConnection)mid.current;
						String temps;
						
						if(scn.isDone())
						{
							Vector tempv = scn.getResponseHeaders();
							int status = 0;
							for(int i = 0; i < tempv.size(); i += 2)
							{
								/* It is case sensitive */
								temps = ((StringBuffer)tempv.elementAt(i)).toString();
								
								if(temps.equals("status"))
									status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
							}
							
							if(status == 11)
							{
								/* It is OK */
								mid.setScreen(new SplashAlert(mid, SplashAlert.OK, "Sporocilo poslano."),
											FormConstants.ALERT);
							}
							else
							{
								/* Display error screen */
								mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Posiljanje ni uspelo."),
											FormConstants.ALERT);
							}
						}
						else
						{
							/* request timed out */
							mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
											FormConstants.ALERT);
						}
					break;
				}
			break;
			/* Alert options */
			case FormConstants.ALERT:
				if(mid.prevForm == FormConstants.LOGIN_CONNECTION)
				{
					mid.setScreen(mid.loginForm(), FormConstants.LOGIN_FORM);
				}
				else if(mid.prevForm == FormConstants.EXIT_SERVER)
				{
					mid.notifyDestroyed();
				}
				else
				{
					mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
				}
			break;
			case FormConstants.LOGIN_FORM:
				switch(itemId)
				{
					case FormConstants.LOGIN_FORM_OK:
						/* login */
						/* Und thas is fail. */
						SplashForm sf = (SplashForm)mid.current;
						mid.setScreen(new SplashConnection(mid, "prijavljam", "socket://" + 
							((TextField)sf.get(1)).getString() + ":42390"),
							FormConstants.LOGIN_CONNECTION);
						scn = (SplashConnection)mid.current;
						scn.addHeader("USERNAME", "owner");
						scn.addHeader("PASSWORD", mid.urlEncode(((TextField)sf.get(2)).getString()));
						scn.connect();
						mid.ip = ((TextField)sf.get(1)).getString();
						
					break;
					case FormConstants.LOGIN_FORM_EXIT:
						mid.notifyDestroyed();
					break;
				}
			break;
			
			case FormConstants.LOGIN_CONNECTION:
				switch(itemId)
				{
					case 0:
						/* Any key to cancel */
						System.out.println("Form cancel");
						((SplashConnection)mid.current).cancelConnection();
						mid.setScreen(mid.loginForm(), FormConstants.LOGIN_FORM);
					break;
					case FormConstants.TRANSACTION_END:
						scn = (SplashConnection)mid.current;
						String temps;
						
						if(scn.isDone())
						{
							Vector tempv = scn.getResponseHeaders();
							int status = 0;
							for(int i = 0; i < tempv.size(); i += 2)
							{
								/* It is case sensitive */
								temps = ((StringBuffer)tempv.elementAt(i)).toString();
								
								if(temps.equals("status"))
									status = Integer.parseInt(((StringBuffer)tempv.elementAt(i + 1)).toString());
								else if(temps.equals("ssid"))
									mid.ssid = ((StringBuffer)tempv.elementAt(i + 1)).toString();
							}
							
							if(status == 11 && mid.ssid != null)
							{
								/* It is OK */
								/* Load menu screen */
								mid.setScreen(mid.mainMenu(), FormConstants.MENU_FORM);
							}
							else
							{
								/* Display error screen */
								mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Neuspela prijava. Poskusite znova."),
											FormConstants.ALERT);
							}
						}
						else
						{
							/* request timed out */
							mid.setScreen(new SplashAlert(mid, SplashAlert.ERROR, "Streznik se ni odzval."),
											FormConstants.ALERT);
						}
					break;
				}
			break;
		}
		
		
	}
}
